<template>
  <div class="list" ref="wrapper">
    <div >
      <div class="area">
        <div class="title border_topbottom">当前城市</div>
        <div class="but_list">
          <div class="but_wrapper">
            <div class="but" @click="currentCity">{{this.currCity}}</div>
          </div>
        </div>
      </div>
      <div class="area">
        <div class="title border_topbottom">热门城市</div>
        <div class="but_list">
          <div class="but_wrapper"
            v-for="item of hot"
            :key="item.id"
            @click="cityClick(item.name)"
          >
            <div class="but">{{item.name}}</div>
          </div>
        </div>
      </div>
      <div class="area" v-for="(item, key) of cities" :key="key" :ref="key">
        <div class="title border_topbottom">{{key}}</div>
          <ul class="item_list"
              v-for="innerItem of item"
              :key="innerItem.id"
              @click="cityClick(innerItem.name)"
            >
             <li class="item border-bottom">{{innerItem.name}}</li>
          </ul>
      </div>
  </div>
  </div>
</template>

<script>
import BScroll from 'better-scroll'
import {mapState, mapMutations} from 'vuex'
export default {
  name: 'CityList',
  props: {
    cities: Object,
    hot: Array,
    letter: String
  },
  computed: {
    ...mapState({
      currCity: 'city'  /*mapState 映射共享属性city到计算属性的currCity中*/
    })
  },
  methods: {
    cityClick (city) {
   /*   this.$store.commit('changeCity', city)*/
      this.changeCity(city)
      this.$router.push('/')
      alert(city)
    },
    ...mapMutations(['changeCity']),
    currentCity () {
      this.$router.push('/')
    }
  },
  watch: {
    letter () {
      if (this.letter) {
        const element = this.$refs[this.letter][0]
        this.scroll.scrollToElement(element)
        console.log(element)
      }
    }
  },
  mounted () {
    this.scroll = new BScroll(this.$refs.wrapper)
  }
}
</script>

<style lang="stylus" scoped>
  @import '~styles/varibles.styl'
  .border_topbottom
    &:before
      border-color: #ccc
    &:after
      border-color: #ccc
  .border_bottom
  &:before
    border-color: #ccc
  .list
    overflow: hidden
    position: absolute
    top: 1.58rem
    left: 0
    right: 0
    bottom: 0
    .title
      padding: .2rem 0 .2rem .2rem
      background: #eee
      color: #666
      font-size: .26rem
    .but_list
      padding: .1rem .6rem .1rem .1rem
      overflow: hidden
      .but_wrapper
        float: left
        width: 33.3%
        .but
          padding: .1rem
          text-align: center
          margin: .1rem
          border: .02rem solid #ccc
          border-radius: .06rem
    .item_list
      .item
        line-height: .76rem
        padding-left: .2rem
</style>
